/**
 * 
 */
package com.qb.mobile.core.actionbar;

import com.qb.mobile.R;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

/**
 * @author <a href="mailto:dangcongthanhtrung@gmail.com">dangtrung</a>
 * 
 *         IntentAction.java class
 * 
 */
public class IntentAction extends AbstractAction {

	private Activity activity;
	private Intent intent;
	private int requestCode;
	private boolean isResult;
	private boolean isCancel;

	public IntentAction(Activity activity, Intent intent, int drawable) {
		super(drawable);
		this.activity = activity;
		this.intent = intent;
		this.requestCode = 0;
	}

	public IntentAction(Activity activity, Intent intent, int drawable,
			int requestCode) {
		super(drawable);
		this.activity = activity;
		this.intent = intent;
		this.requestCode = requestCode;
	}

	public IntentAction(Activity activity, Intent intent, int drawable,
			boolean isResult, boolean isCancel) {
		super(drawable);
		this.activity = activity;
		this.intent = intent;
		this.requestCode = 0;
		this.isResult = isResult;
		this.isCancel = isCancel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mobile.core.actionbar.Action#performAction(android.view.View)
	 */
	@Override
	public void performAction(View view) {
		try {
			if (requestCode == 0) {
				if (isResult && !isCancel) {
					activity.setResult(Activity.RESULT_OK, intent);
					activity.finish();
				} else if (!isResult && isCancel) {
					activity.setResult(Activity.RESULT_CANCELED, intent);
					activity.finish();
				} else {
					activity.startActivity(intent);
				}
			} else {
				final int result = requestCode;
				activity.startActivityForResult(intent, result);
			}

		} catch (ActivityNotFoundException e) {
			Toast.makeText(
					activity.getBaseContext(),
					activity.getBaseContext().getText(
							R.string.actionbar_activity_not_found),
					Toast.LENGTH_SHORT).show();
		}

	}

}
